package com.aischool.webapp.teacher;

import com.aischool.common.model.AiNote;
import com.aischool.params.AiNotesParams;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiNotesService;
import com.aischool.utils.AppUtil;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.aischool.utils.StringUtil;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

@RouteMapping(url = "/front/teacher/notes")
public class AiNotesController extends BaseTeacherController {

	
	@Inject
	AiNotesService aiNotesService;
	

	public void getPage() {
		// 结果类
		JsonResult jr = new JsonResult();
		// 参数类
		AiNotesParams params = new AiNotesParams();
		
		// 获取参数
		int login_org_id = this.getAttrForInt("login_org_id");	
		int login_class_id = this.getAttrForInt("login_class_id");		
		
		params.setPageNo(getParaToInt("pageNo", 1));
		params.setPageSize(getParaToInt("pageSize", AppUtil.Default_PageSize));
		
		params.setOrgId(login_org_id);
		params.setClassId(login_class_id);
		
		params.setState(1);

		// 获取分页
		Object pageData = aiNotesService.getPage(params);
		
		jr.setDicKV("pageData", pageData);
		
		
		jr.setEnum(JsonCodeEnum.Success);
		renderJson(jr.Dic);
		
	}
	
	public void detaile() {

		int id = getInt("id", 0);
		if (id>0) {
			Record rd = aiNotesService.getOne(id);
			if (StrKit.notBlank(rd.getStr("note_pics"))) {
				this.set("note_pics", StringUtil.trim(rd.getStr("note_pics"), ';').split(";"));
			}
			if (StrKit.notBlank(rd.getStr("node_videos"))) {
				this.set("node_videos", StringUtil.trim(rd.getStr("node_videos"), ';').split(";"));
			}
			this.setAttr("add_time", DateUtil.getDate(rd.getDate("add_time"), "yyyy年MM月dd日"));
			this.setAttr("note_rd", rd);
			//System.err.println(rd);
		}
		render(view_path + "/notes_detaile.html");
	}
	
	public void save() {
		JsonResult jr =new JsonResult();
		
		int login_user_id = this.getAttrForInt("login_user_id");
		int login_org_id = this.getAttrForInt("login_org_id");	
		int login_class_id = this.getAttrForInt("login_class_id");
		
		AiNote info = getModel(AiNote.class, "", true);
		
		if (StrKit.isBlank(info.getNoteTitle())) {
			jr.appendMsg("请输入标题！");
		}else if(StrKit.isBlank(info.getNoteDesc())) {
			jr.appendMsg("请输入内容！");
		}else if (info.getTypeId() == 2 && StrKit.isBlank(this.get("start_time"))) {
			jr.appendMsg("请输入开始时间！");
		}else if(info.getTypeId() == 2 && StrKit.isBlank(this.get("end_time"))) {
			jr.appendMsg("请输入结束时间！");
		}else if(info.getTypeId() == 2 && StrKit.isBlank(info.getShowAddress())) {
			jr.appendMsg("请输入活动地址 ！");
		}else {
			if (info.getTypeId() == 2) {
				if (!StrKit.isBlank(this.get("start_time"))) {
					info.setStartTime(DateUtil.getTimestamp(this.get("start_time"), "yyyy-MM-dd'T'HH:mm"));
				}
				if (!StrKit.isBlank(this.get("end_time"))) {
					info.setEndTime(DateUtil.getTimestamp(this.get("end_time"), "yyyy-MM-dd'T'HH:mm"));	
				}
				
			}
			info.setUpdateTime(DateUtil.getTimestamp());			
			info.removeNullValueAttrs();
			
			
			boolean bf = false;
			
			if (info.getNoteId() != null && info.getNoteId() > 0) {
				bf = info.update();
			}else {
				info.setOrgId(login_org_id);
				info.setClassId(login_class_id);
				info.setAddUserid(login_user_id);
				info.setAddTime(DateUtil.getTimestamp());
				info.setNoteState(1);
				
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}
		
		renderJson(jr.Dic);
	}
}
